package view.hero
{
import core.Language;
import core.model.PlayerModel;

import framework.view.components.button.ButtonProxy;
import framework.view.components.container.IRenderProxy;

import values.avatar.IAvatarValue;
import values.item.EquipmentItemValue;

import view.component.window.PaginationWindowProxy;

/**
 * HeroEquipSelectionUIProxy
 * @author jliang
 *
 */
public class HeroEquipSelectionUIProxy extends PaginationWindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function HeroEquipSelectionUIProxy()
	{
		super();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
	
	protected var heros:Vector.<Object>;   /* { avatar: IAvatarValue, equipment: EquipmentItemValue } */
	
	protected function get okBtn():ButtonProxy
	{
		return proxys["okBtn"];
	}
	
	override public function dispose():void
	{
		heros.length = 0;
		heros = null;
		selectedHero = null;
		equipment = null;
		super.dispose();
	}
	
	protected var selectedHero:IAvatarValue;
	
	protected var equipment:EquipmentItemValue;
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get repeaterItemRenderClass():Class
	{
		return HeroEquipSelectionItemRenderProxy;
	}
	
	override protected function clickToGoPrevHandler():void
	{
		updateList(heros, heros.length, currentPage - 1);
	}
	
	override protected function clickToGoNextHandler():void
	{
		updateList(heros, heros.length, currentPage + 1);
	}
	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"okBtn", classRef: ButtonProxy, id:"okBtn", text:Language.getText("@ok_button_label"), onClick:clickToSelectHandler }
		].concat(super.proxyMapping);
	}
	
	override protected function listItemSelectedHandler(render:IRenderProxy):void
	{
		selectedHero = render.data.avatar;
		
		okBtn.enabled = true;
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function showHero(equipment:EquipmentItemValue):void
	{		
		this.equipment = equipment;
		
		initData();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function initData():void
	{
		heros = new Vector.<Object>();
		
		var allHeros:Array = playerModel.myForce.allHeros;
		
		for each (var hero:IAvatarValue in allHeros)
		{
			//过滤不能装备该物品的英雄
			if (!hero.avatarType.canSupportEquip(equipment.equipType)) continue;			
			
			var heroEquipment:EquipmentItemValue = hero.getEquipmentByPosition(equipment.position);
			heros.push({ avatar: hero, equipment: heroEquipment });
		}
		
		var pageCount:int = Math.ceil(heros.length / preCount);
		
		var page:int = currentPage;
		if (pageCount - 1 < currentPage)
		{
			page = pageCount - 1;
			if (page < 0)
			{
				page = 0;
			}
		}
		updateList(heros, heros.length, page);
		
		skin.msgLabel.visible = (heros.length == 0);
		
		okBtn.enabled = false;
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function clickToSelectHandler():void
	{
		if (selectedHero)
		{
			selectedHero.addEquipment(equipment);
		}		
		this.close();
	}
}
}





